package com.acp.service.impl;

import com.acp.context.Config;
import com.acp.dao.acpo.*;
import com.acp.model.DcsCollectCode;
import com.acp.model.DcsFeedback;
import com.acp.model.DcsTask;
import com.acp.model.DcsTaskPerformance;
import com.acp.param.DcsTaskParam;
import com.acp.result.PageResult;
import com.acp.result.ResultInfo;
import com.acp.result.ResultUtil;
import com.acp.service.DcsTaskService;
import com.acp.util.UUIDBuild;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.construction
 * @Description: 任务实现类
 * @author: 张星亮
 * @date: 2019/4/5
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
public class DcsTaskServiceImpl implements DcsTaskService {
    @Resource
    private DcsTaskMapper dcsTaskMapper;

    @Resource
    private DcsTaskPerformanceMapper dcsTaskPerformanceMapper;

    @Resource
    private DcsCollectCodeMapper dcsCollectCodeMapper;

    @Resource
    private DcsFeedbackMapper dcsFeedbackMapper;

    @Resource
    private DcsPerformanceMapper dcsPerformanceMapper;

    /**
     * 添加
     * @param dcsTaskParam
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo addDcsTask(DcsTaskParam dcsTaskParam,String url,String photoName) {


        String endTime = dcsTaskParam.getEnd_time();
        String remind = endTime + " 任务时间截至，请尽快完成！";
        DcsTask dcsTask = DcsTask.builder().id(UUIDBuild.getUUID()).url(url).photoName(photoName).group_code(dcsTaskParam.getGroup_code()).street(dcsTaskParam.getStreet()).community(dcsTaskParam.getCommunity()).collect_name(dcsTaskParam.getCollect_name()).end_time(dcsTaskParam.getEnd_time()).start_time(dcsTaskParam.getStart_time()).group_name(dcsTaskParam.getGroup_name()).task_code(dcsTaskParam.getTask_code()).task_description(dcsTaskParam.getTask_description()).task_feedback(dcsTaskParam.getTask_feedback()).task_number(dcsTaskParam.getTask_number()).task_name(dcsTaskParam.getTask_name()).task_remind(remind).build();
        int i = dcsTaskMapper.insert(dcsTask);
        List<DcsCollectCode> list = dcsCollectCodeMapper.selectCountByCode(dcsTaskParam.getTask_code());
        String number = String.valueOf(list.size());
        DcsTaskPerformance dtp = DcsTaskPerformance.builder().id(UUIDBuild.getUUID()).group_code(dcsTaskParam.getGroup_code()).task(dcsTaskParam.getTask_code()).group_name(dcsTaskParam.getGroup_name()).start_time(dcsTaskParam.getStart_time()).end_time(dcsTaskParam.getEnd_time()).total_count(number).build();
        int a = dcsTaskPerformanceMapper.insert(dtp);
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);

        }
        return ResultUtil.createWarning(Config.MESSAGE, 911, null);
    }

    /**
     * 查询
     * @param list
     * @param name
     * @return 返回PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public PageResult queryDcsTaskList(List<DcsTaskParam> list, String name) {
        if (list != null && list.size() != 0) {
            for (DcsTaskParam dcsTaskParam : list) {
                name = dcsTaskParam.getTask_name();
                List<DcsTask> dcsTasks = dcsTaskMapper.queryDcsTaskList(name);
                if (dcsTasks.size() > 0) {
                    for (int a = 0; a < dcsTasks.size(); a++) {
                        String id = dcsTasks.get(a).getId();
                        List<DcsFeedback> list1 = dcsFeedbackMapper.selectById(id);
                        dcsTasks.get(a).setList(list1);
                        String code = dcsTasks.get(a).getTask_code();
                        List<DcsCollectCode> listcode = dcsCollectCodeMapper.selectCountByCode(code);

                        dcsTasks.get(a).setList1(listcode);
                    }
                }
                PageInfo<DcsTask> pageInfo = new PageInfo<>(dcsTasks);
                Long count = pageInfo.getTotal();
                return PageResult.success(dcsTasks, count);
            }
        }
        List<DcsTask> dcsTasks = dcsTaskMapper.queryDcsTaskList(name);

        PageInfo<DcsTask> pageInfo = new PageInfo<>(dcsTasks);
        Long count = pageInfo.getTotal();
        return PageResult.success(dcsTasks, count);
    }

    /**
     * 查询所有
     * @param name
     * @return 返回PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public PageResult queryDcsTaskRemindList(String name) {
        List<DcsTask> taskList = dcsTaskMapper.queryDcsTaskRemindList(name);
        List<Integer> l = new ArrayList<>();
        if (taskList != null && taskList.size() != 0) {
            for (int a = 0; taskList.size() > a; a++) {
                DcsTask dcsTask = taskList.get(a);
                String format = "yyyy-MM-dd";
                try {
                    Date date = new SimpleDateFormat(format).parse(dcsTask.getEnd_time());
                    Date newDate = new Date();
                    Calendar rightNow = Calendar.getInstance();
                    rightNow.setTime(newDate);
                    rightNow.add(Calendar.DAY_OF_MONTH, +1);
                    Date dt1 = rightNow.getTime();
                    rightNow.setTime(newDate);
                    rightNow.add(Calendar.DAY_OF_MONTH, -1);
                    Date dt2 = rightNow.getTime();
                    if (date.after(dt1) || date.before(dt2)) {
                        l.add(a);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
        }
        if (l.size() > 0) {
            for (int b = l.size() - 1; b >= 0; b--) {
                taskList.remove(taskList.get(l.get(b)));
            }
        }
        PageInfo<DcsTask> pageInfo = new PageInfo<>(taskList);
        Long count = pageInfo.getTotal();
        return PageResult.success(taskList, count);
    }

    /**
     * 删除
     * @param list
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo deleteDcsTaskById(List<DcsTaskParam> list) {
        if (list != null && list.size() != 0) {
            for (DcsTaskParam dcsTaskParam : list) {
                String id = dcsTaskParam.getId();
                String taskCode = dcsTaskParam.getTask_code();
                int i = dcsTaskMapper.deleteByPrimaryKey(id);
                int a = dcsTaskPerformanceMapper.deletePerformanceByCode(taskCode);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

    /**
     * 修改
     * @param dcsTaskParam
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo editDcsTask(DcsTaskParam dcsTaskParam,String url,String photoName) {
        String endTime = dcsTaskParam.getEnd_time();
        String remind = endTime + " 任务时间截至，请尽快完成！";
        DcsTask dcsTask = DcsTask.builder().id(dcsTaskParam.getId()).url(url).photoName(photoName).group_code(dcsTaskParam.getGroup_code()).street(dcsTaskParam.getStreet()).community(dcsTaskParam.getCommunity()).collect_name(dcsTaskParam.getCollect_name()).end_time(dcsTaskParam.getEnd_time()).start_time(dcsTaskParam.getStart_time()).group_name(dcsTaskParam.getGroup_name()).task_code(dcsTaskParam.getTask_code()).task_description(dcsTaskParam.getTask_description()).task_feedback(dcsTaskParam.getTask_feedback()).task_number(dcsTaskParam.getTask_number()).task_name(dcsTaskParam.getTask_name()).task_remind(remind).build();
        int i = dcsTaskMapper.updateByPrimaryKey(dcsTask);
        List<DcsCollectCode> list = dcsCollectCodeMapper.selectCountByCode(dcsTaskParam.getTask_code());
        String number = String.valueOf(list.size());
        int stayCount = dcsPerformanceMapper.selectStarCount(dcsTaskParam.getTask_code());
        String stCount = String.valueOf(stayCount);
        String comCount = String.valueOf(list.size()-stayCount);
        DcsTaskPerformance dtp = DcsTaskPerformance.builder().group_code(dcsTaskParam.getGroup_code()).task(dcsTaskParam.getTask_code()).group_name(dcsTaskParam.getGroup_name()).start_time(dcsTaskParam.getStart_time()).end_time(dcsTaskParam.getEnd_time()).total_count(number).stay_count(stCount).complete_count(comCount).build();
        int b = dcsTaskPerformanceMapper.updatePerformance(dtp);
        if (i > 0) {
            return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
        }
        return ResultUtil.createWarning(Config.MESSAGE, 911, null);
    }

    /**
     * 删除多条反馈
     * @param list
     * @return 返回ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @Override
    public ResultInfo deleteByfeedback(List<DcsTaskParam> list) {
        if (list != null && list.size() != 0) {
            for (DcsTaskParam dcsTaskParam : list) {
                String id = dcsTaskParam.getId();
                int i = dcsTaskMapper.deleteById(id);
            }
        }
        return ResultUtil.createSuccess(Config.MESSAGE, 906, null);
    }

}
